package org.book.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.book.dao.BorrowBean;
import org.book.service.BorrowService;


/**
 * Servlet implementation class BorrowUpdateServlet
 */
@WebServlet("/BorrowUpdateServlet")
public class BorrowUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BorrowUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		
		BorrowService borrowService = new BorrowService();
		
		String pageCurrent2 = request.getParameter("pageCurrent2");
		
		//归还修改时间
		String record_id = request.getParameter("record_id");
		
		String no = request.getParameter("no");
		
		String rentTime = request.getParameter("rentTime");
		String returnTime = request.getParameter("returnTime");
		String ISBN = request.getParameter("ISBN");
		String cardNumber = request.getParameter("cardNumber");
		//修改
		if(no!=null&&rentTime!=null&&returnTime!=null&&ISBN!=null&&cardNumber!=null){
			
			int id = Integer.parseInt(no);
			BorrowBean stu = new BorrowBean(rentTime,returnTime,ISBN,cardNumber);
			
			//修改ISBN和cardNumber 由于rent_record表中没有这两项
			//将通过ISBN和cardNumber查询对应表中的book_id和user_id
			//然后更新rent_record表中的book_id和user_id
			//从而达到修改借阅记录对应的图书和用户
			boolean bool = borrowService.update(id, stu);
			
			if(bool) {
				System.out.println("修改成功");
				//保存修改前的页码
				request.setAttribute("pageCurrent2", pageCurrent2);
			}else {
				System.out.println("修改失败");
			}
			
			
			
			
		}
		//删除
		else if(no!=null&&rentTime==null){
			int id = Integer.parseInt(no);
			boolean bool = borrowService.remove(id);
			if(bool) {
				System.out.println("撤销成功");
				//保存删除前页码
				request.setAttribute("pageCurrent2", pageCurrent2);
			}else {
				System.out.println("撤销失败");
				
			}
		}
		
		else if(record_id!=null){
			int id = Integer.parseInt(record_id);
			System.out.println(id);
			boolean bool = borrowService.update(id);
			if(bool) {
				System.out.println("归还成功");
			}else {
				System.out.println("已归还");
			}
		}
		//错误
		else {
			System.out.println("输入错误");
		}
		request.getRequestDispatcher("BorrowServlet").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
